var WxSearch = require('../../views/wxSearchView/wxSearchView.js');
Page({
    data: {
        tabs: ['食谱查询', '用药助手'],
        stv: {
            windowWidth: 0,
            lineWidth: 0,
            offset: 0,
            tStart: false
        },
        activeTab: 0,
        hotSearch: [
            '糖尿病', '高血压', "台湾牛肉面", "猪肉", '秋葵', '金华'
        ],
        searchMapping: [
            '湖北', '湖南', '北京', "南京"
        ]

    },
    onLoad: function(options) {
        var that = this;
        WxSearch.init(
            that, // 本页面一个引用
            that.data.hotSearch, // 热点搜索推荐，[]表示不使用
            that.data.searchMapping, // 搜索匹配，[]表示不使用
            that.mySearchFunction, // 提供一个搜索回调函数
            that.myGobackFunction //提供一个返回回调函数
        );
        try {
            let {
                tabs
            } = this.data;
            var res = wx.getSystemInfoSync()
            this.windowWidth = res.windowWidth;
            this.data.stv.lineWidth = this.windowWidth / this.data.tabs.length;
            this.data.stv.windowWidth = res.windowWidth;
            this.setData({
                stv: this.data.stv
            })
            this.tabsCount = tabs.length;
        } catch (e) {}

    },
    // 转发函数,固定部分
    wxSearchInput: WxSearch.wxSearchInput, // 输入变化时的操作
    wxSearchKeyTap: WxSearch.wxSearchKeyTap, // 点击提示或者关键字、历史记录时的操作
    wxSearchDeleteAll: WxSearch.wxSearchDeleteAll, // 删除所有的历史记录
    wxSearchConfirm: WxSearch.wxSearchConfirm, // 搜索函数
    wxSearchClear: WxSearch.wxSearchClear, // 清空函数

    // 搜索回调函数  
    mySearchFunction: function(value) {
        // do your job here
        // 跳转
        if (this.data.activeTab == 0) {
            wx.redirectTo({
                url: '../food/food?searchValue=' + value
            })
        } else {
            wx.redirectTo({
                url: '../medicine/medicine?searchValue=' + value
            })
        }
        console.log(value)
    },

    // 返回回调函数
    myGobackFunction: function() {
        // do your job here
        // 跳转
        wx.redirectTo({
            url: '../index/index'
        })
    },
    handlerStart(e) {
        let {
            clientX,
            clientY
        } = e.touches[0];
        this.startX = clientX;
        this.tapStartX = clientX;
        this.tapStartY = clientY;
        this.data.stv.tStart = true;
        this.tapStartTime = e.timeStamp;
        this.setData({
            stv: this.data.stv
        })
    },
    handlerMove(e) {
        let {
            clientX,
            clientY
        } = e.touches[0];
        let {
            stv
        } = this.data;
        let offsetX = this.startX - clientX;
        this.startX = clientX;
        stv.offset += offsetX;
        if (stv.offset <= 0) {
            stv.offset = 0;
        } else if (stv.offset >= stv.windowWidth * (this.tabsCount - 1)) {
            stv.offset = stv.windowWidth * (this.tabsCount - 1);
        }
        this.setData({
            stv: stv
        });
    },
    handlerCancel(e) {

    },
    handlerEnd(e) {
        let {
            clientX,
            clientY
        } = e.changedTouches[0];
        let endTime = e.timeStamp;
        let {
            tabs,
            stv,
            activeTab
        } = this.data;
        let {
            offset,
            windowWidth
        } = stv;
        //快速滑动
        if (endTime - this.tapStartTime <= 300) {
            //向左
            if (Math.abs(this.tapStartY - clientY) < 50) {
                if (this.tapStartX - clientX > 5) {
                    if (activeTab < this.tabsCount - 1) {
                        this.setData({
                            activeTab: ++activeTab
                        })
                    }
                } else {
                    if (activeTab > 0) {
                        this.setData({
                            activeTab: --activeTab
                        })
                    }
                }
                stv.offset = stv.windowWidth * activeTab;
            } else {
                //快速滑动 但是Y距离大于50 所以用户是左右滚动
                let page = Math.round(offset / windowWidth);
                if (activeTab != page) {
                    this.setData({
                        activeTab: page
                    })
                }
                stv.offset = stv.windowWidth * page;
            }
        } else {
            let page = Math.round(offset / windowWidth);
            if (activeTab != page) {
                this.setData({
                    activeTab: page
                })
            }
            stv.offset = stv.windowWidth * page;
        }
        stv.tStart = false;
        this.setData({
            stv: this.data.stv
        })
    },
    _updateSelectedPage(page) {
        let {
            tabs,
            stv,
            activeTab
        } = this.data;
        activeTab = page;
        this.setData({
            activeTab: activeTab
        })
        this.changeSearchData(activeTab);
        stv.offset = stv.windowWidth * activeTab;
        this.setData({
            stv: this.data.stv
        })
    },
    handlerTabTap(e) {
        this._updateSelectedPage(e.currentTarget.dataset.index);
    },
    changeSearchData(activeTab) {
        console.log(activeTab)
        if (activeTab == 1) {
            this.setData({
                hotSearch: [
                    '药品1', '药品2', "药品3", "药品4", '药品5', '药品5'
                ],
                searchMapping: [
                    '药品1', '药品2', '药品3', "药品4"
                ]
            })
        } else {
            this.setData({
                hotSearch: [
                    '糖尿病', '高血压', "台湾牛肉面", "猪肉", '秋葵', '金华'
                ],
                searchMapping: [
                    '湖北', '湖南', '北京', "南京"
                ]
            })
        }
        var that = this;
        WxSearch.init(
            that, // 本页面一个引用
            that.data.hotSearch, // 热点搜索推荐，[]表示不使用
            that.data.searchMapping, // 搜索匹配，[]表示不使用
            that.mySearchFunction, // 提供一个搜索回调函数
            that.myGobackFunction //提供一个返回回调函数
        );
    }
})